import gevent
from gevent import monkey
monkey.patch_all()
import requests
import json
from autoTask.pinduoduo.Login import PinDuoDuo


class Example(PinDuoDuo):
    def __init__(self, account, password, beginDate, endDate):
        super().__init__(account, password,)
        self.beginDate = beginDate
        self.endDate = endDate

    def run(self):
        self.action()
        self.get_pilot()

    def headers(self):
        cookies = ''
        for key, value in self.page.cookies.items():
            if key in ['JSESSIONID', 'PASS_ID']:
                cookies += f"{key}={value};"
        return {
            'Host': 'mms.pinduoduo.com',
            'content-type': 'application/json',
            'Accept-Encoding': 'gzip, deflate, br',
            'Cookie': cookies
        }

    def get_pilot(self):
        url = 'https://mms.pinduoduo.com/sydney/api/mallNavigator/queryUpgradeMallNavigatorList'
        data = {
            "beginDate": self.beginDate,
            "endDate": self.endDate
        }
        response = requests.post(url, headers=self.headers(), data=json.dumps(data))
        response_json = response.json()
        for row in response_json['result']:
            if row['mallId'] is None:
                continue
            dic = {
                '店铺名称': row['mallName'],
                '日期': row['pt'],
                '求助率': round(row['ptHelpRate1m'] * 100, 2),
                '领航员综合分行业排名': int(row['scoreRk']),
            }
            print(dic)


if __name__ == '__main__':
    e = Example('赫巴兹家居官方旗舰店数据', 'Yzt123456', '2023-09-07', '2023-09-08')
    e.run()
